package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsEnableDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 添加文章存入草稿，提交发布
     * @param wmNewsDtoSave
     * @param draft
     * @return
     */
    @ApiOperation("新增文章接口")
    @PostMapping("/submit")
    public ResultVo save(@RequestBody WmNewsDtoSave wmNewsDtoSave,boolean draft){
        Long id = wmNewsService.addWmNews(wmNewsDtoSave,draft);
        return ResultVo.ok(id);
    }

    /**
     *内容列表分页查询
     * @param wmMaterialPageRequestDto 接收前端传的参数
     * @return 返回分页参数
     */
    @ApiOperation("内容分页查询")
    @PostMapping("/list")
    public PageResultVo findPage(@RequestBody WmMaterialPageRequestDto wmMaterialPageRequestDto){
        PageResultVo pageResultVo = wmNewsService.findPage(wmMaterialPageRequestDto);
        return pageResultVo;
    }

    /**
     * 根据id查询文章
     * @param wmNewsId 文章id
     * @return 返回查询到的文章数据
     */
    @ApiOperation("根据id进行查询")
    @GetMapping("/one/{wmNewsId}")
    public ResultVo<WmNews> findById(@PathVariable Long wmNewsId){
        return ResultVo.ok(wmNewsService.findById(wmNewsId));
    }

    /**
     * 根据文章id执行删除
     * @param id 文章id
     * @return 返回是否删除成功提示
     */
    @ApiOperation("根据id进行删除操作")
    @GetMapping("/del_news/{id}")
    public ResultVo del(@PathVariable Long id){
        wmNewsService.delete(id);
        return ResultVo.ok("删除文章成功！");
    }

    /**
     * 文章上下架接口
     * @param wmNewsEnableDto 接收页面传来的文章id
     * @return 返回上下是否成功提示
     */
    @ApiOperation("文章上架")
    @PostMapping("/down_or_up")
    public ResultVo  updateEnable(@RequestBody WmNewsEnableDto wmNewsEnableDto){

        return wmNewsService.updateEnable(wmNewsEnableDto);
    }

    /**
     * 媒体审核分页查询
     * @param wmNewsDtoSave 接收页面传过来的数据
     * @return 返回查询到的分页结果
     */
    @ApiOperation("媒体审核分页查询接口")
    @PostMapping("/list_vo")
    public PageResultVo pageList(@RequestBody WmNewsDtoSave wmNewsDtoSave){
        //调用service层进行查询
        return wmNewsService.findPageList(wmNewsDtoSave);
    }

    /**
     * 根据文章id查询文章接口
     * @param id 文章id
     * @return 返回文章对象
     */
    @ApiOperation("根据文章id查询文章接口")
    @GetMapping("/one_vo/{id}")
    public ResultVo findByWmNewsId(@PathVariable Long id){
        return ResultVo.ok(wmNewsService.getById(id));
    }

    /**
     * 人工审核通过接口
     * @param wmNewsDtoSave
     * @return
     */
    @ApiOperation("人工审核通过接口")
    @PostMapping("/auth_pass")
    public ResultVo updateWmNews(@RequestBody WmNewsDtoSave wmNewsDtoSave){
        if (wmNewsService.updateStatus(wmNewsDtoSave)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
    /**
     * 人工审核驳回接口
     * @param wmNewsDtoSave
     * @return
     */
    @ApiOperation("人工审核驳回接口")
    @PostMapping("/auth_fail")
    public ResultVo updateWmNewsFail(@RequestBody WmNewsDtoSave wmNewsDtoSave){
        if (wmNewsService.updateStatusFail(wmNewsDtoSave)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
}

